<?php
class ModelContentContent extends Model {
	public function addContent($data) {
		$this->db->query("INSERT INTO " . DB_PREFIX . "content SET date_available = '" . $this->db->escape($data['date_available']) . "', status = '" . (int)$data['status'] . "', sort_order = '" . (int)$data['sort_order'] . "', date_added = NOW()");
		
		$content_id = $this->db->getLastId();
		
		if (isset($data['image'])) {
			$this->db->query("UPDATE " . DB_PREFIX . "content SET image = '" . $this->db->escape($data['image']) . "' WHERE content_id = '" . (int)$content_id . "'");
		}
		
		foreach ($data['content_description'] as $language_id => $value) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "content_description SET content_id = '" . (int)$content_id . "', language_id = '" . (int)$language_id . "', name = '" . $this->db->escape($value['name']) . "', meta_keywords = '" . $this->db->escape($value['meta_keywords']) . "', meta_description = '" . $this->db->escape($value['meta_description']) . "', description = '" . $this->db->escape($value['description']) . "'");
		}
		
		if (isset($data['content_image'])) {
			foreach ($data['content_image'] as $image) {
        		$this->db->query("INSERT INTO " . DB_PREFIX . "content_image SET content_id = '" . (int)$content_id . "', image = '" . $this->db->escape($image) . "'");
			}
		}
		
		if (isset($data['content_category'])) {
			foreach ($data['content_category'] as $category_id) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "content_to_category SET content_id = '" . (int)$content_id . "', category_id = '" . (int)$category_id . "'");
			}
		}
	
		if ($data['keyword']) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "url_alias SET query = 'content_id=" . (int)$content_id . "', keyword = '" . $this->db->escape($data['keyword']) . "'");
		}
		
		$this->cache->delete('content');
	}
	
	public function editContent($content_id, $data) {
		$this->db->query("UPDATE " . DB_PREFIX . "content SET date_available = '" . $this->db->escape($data['date_available']) . "', status = '" . (int)$data['status'] . "', sort_order = '" . (int)$data['sort_order'] . "', date_modified = NOW() WHERE content_id = '" . (int)$content_id . "'");

		if (isset($data['image'])) {
			$this->db->query("UPDATE " . DB_PREFIX . "content SET image = '" . $this->db->escape($data['image']) . "' WHERE content_id = '" . (int)$content_id . "'");
		}
		
		$this->db->query("DELETE FROM " . DB_PREFIX . "content_description WHERE content_id = '" . (int)$content_id . "'");
		
		foreach ($data['content_description'] as $language_id => $value) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "content_description SET content_id = '" . (int)$content_id . "', language_id = '" . (int)$language_id . "', name = '" . $this->db->escape($value['name']) . "', meta_keywords = '" . $this->db->escape($value['meta_keywords']) . "', meta_description = '" . $this->db->escape($value['meta_description']) . "', description = '" . $this->db->escape($value['description']) . "'");
		}
       
		$this->db->query("DELETE FROM " . DB_PREFIX . "content_image WHERE content_id = '" . (int)$content_id . "'");
		
		if (isset($data['content_image'])) {
			foreach ($data['content_image'] as $image) {
        		$this->db->query("INSERT INTO " . DB_PREFIX . "content_image SET content_id = '" . (int)$content_id . "', image = '" . $this->db->escape($image) . "'");
			}
		}
		
		$this->db->query("DELETE FROM " . DB_PREFIX . "content_to_category WHERE content_id = '" . (int)$content_id . "'");
		
		if (isset($data['content_category'])) {
			foreach ($data['content_category'] as $category_id) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "content_to_category SET content_id = '" . (int)$content_id . "', category_id = '" . (int)$category_id . "'");
			}		
		}
        
		$this->db->query("DELETE FROM " . DB_PREFIX . "url_alias WHERE query = 'content_id=" . (int)$content_id. "'");
		
		if ($data['keyword']) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "url_alias SET query = 'content_id=" . (int)$content_id . "', keyword = '" . $this->db->escape($data['keyword']) . "'");
		}
	
		$this->cache->delete('content');
	}
	
	public function copyContent($content_id) {
		$query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "content p LEFT JOIN " . DB_PREFIX . "content_description pd ON (p.content_id = pd.content_id) WHERE p.content_id = '" . (int)$content_id . "' AND pd.language_id = '" . (int)$this->config->get('config_language_id') . "'");
		
		if ($query->num_rows) {
			$data = array();
			
			$data = $query->row;
			
			$data = array_merge($data, array('content_description' => $this->getContentDescriptions($content_id)));
				
			$data['keyword'] = '';
						
			$data['content_image'] = array();
			
			$results = $this->getContentImages($content_id);
			
			foreach ($results as $result) {
				$data['content_image'][] = $result['image'];
			}
			
			$data = array_merge($data, array('product_category' => $this->getProductCategories($product_id)));
			
			$this->addProduct($data);
		}
	}
	
	public function deleteContent($content_id) {
		$this->db->query("DELETE FROM " . DB_PREFIX . "content WHERE content_id = '" . (int)$content_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "content_description WHERE content_id = '" . (int)$content_id . "'");
		
        $this->db->query("DELETE FROM " . DB_PREFIX . "content_image WHERE content_id = '" . (int)$content_id . "'");
		
        $this->db->query("DELETE FROM " . DB_PREFIX . "content_to_category WHERE content_id = '" . (int)$content_id . "'");
		
        $this->db->query("DELETE FROM " . DB_PREFIX . "url_alias WHERE query = 'content_id=" . (int)$content_id. "'");
		
		$this->cache->delete('content');
	}
	
	public function getContent($content_id) {
		$query = $this->db->query("SELECT DISTINCT *, (SELECT keyword FROM " . DB_PREFIX . "url_alias WHERE query = 'content_id=" . (int)$content_id . "') AS keyword FROM " . DB_PREFIX . "content p LEFT JOIN " . DB_PREFIX . "content_description pd ON (p.content_id = pd.content_id) WHERE p.content_id = '" . (int)$content_id . "' AND pd.language_id = '" . (int)$this->config->get('config_language_id') . "'");
				
		return $query->row;
	}
	
	public function getContents() {
        $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "content p LEFT JOIN " . DB_PREFIX . "content_description pd ON (p.content_id = pd.content_id) WHERE pd.language_id = '" . (int)$this->config->get('config_language_id') . "'");
				
		return $query->rows;
	}
    
	public function getContentsByKeyword($keyword) {
		if ($keyword) {
			$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "content p LEFT JOIN " . DB_PREFIX . "content_description pd ON (p.content_id = pd.content_id) WHERE pd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND (LCASE(pd.name) LIKE '%" . $this->db->escape(strtolower($keyword)) . "%')");
									  
			return $query->rows;
		} else {
			return array();	
		}		
	} 
	
	public function getContentsByCategoryId($category_id, $sort = '', $order = '', $limit = 0, $page = 0){
	    $sql = "SELECT p.*, pd.* FROM " . DB_PREFIX . "content p " .
            " LEFT JOIN " . DB_PREFIX . "content_description pd ON (p.content_id = pd.content_id) " .
            " LEFT JOIN " . DB_PREFIX . "content_to_category p2c ON (p.content_id = p2c.content_id) " .
            " LEFT JOIN " . DB_PREFIX . "category_content cc ON (p2c.category_id = cc.category_id) " .
            " WHERE pd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND p2c.category_id = '" . (int)$category_id . "' AND p.status = '1'";
        
        if($sort){
             $sql .= ' ORDER BY ' . $sort;
        }
        else{
            $sql .= " ORDER BY pd.name";
        }
        
        if($order){
            $sql .= ' ' . $order;
        }
        else{
            $sql .= " ASC";
        }
           
        if((int)$limit > 0){
            $start = ($page - 1) * $limit;
            $start = ($start < 0) ? 0 : $start;
        
            $sql .= " LIMIT " . (int)$start . "," . (int)$limit;
        }
		    
        $query = $this->db->query($sql);
								  
		return $query->rows;
	} 
	
	public function getContentDescriptions($content_id) {
		$content_description_data = array();
		
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "content_description WHERE content_id = '" . (int)$content_id . "'");
		
		foreach ($query->rows as $result) {
			$content_description_data[$result['language_id']] = array(
				'name'             => $result['name'],
				'meta_keywords'    => $result['meta_keywords'],
				'meta_description' => $result['meta_description'],
				'description'      => $result['description']
			);
		}
		
		return $content_description_data;
	}
   
	public function getContentImages($content_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "content_image WHERE content_id = '" . (int)$content_id . "'");
		
		return $query->rows;
	}

	public function getContentCategories($content_id) {
		$content_category_data = array();
		
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "content_to_category WHERE content_id = '" . (int)$content_id . "'");
		
		foreach ($query->rows as $result) {
			$content_category_data[] = $result['category_id'];
		}

		return $content_category_data;
	}
    
    public function getContentCategoriesFull($content_id) {	
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "content_to_category cc LEFT JOIN category_content_description cd ON (cc.category_id = cd.category_id) WHERE content_id = '" . (int)$content_id . "' AND cd.language_id = '" . (int)$this->config->get('config_language_id') . "'");
		
        $content_category_data = array();
		foreach ($query->rows as $result) {
			$content_category_data[] = $result;
		}

		return $content_category_data;
	}

    public function getTotalContentByCategory($category_id) {
        $sql = "SELECT COUNT(*) AS total FROM " . DB_PREFIX . "content p " .
            " LEFT JOIN " . DB_PREFIX . "content_description pd ON (p.content_id = pd.content_id) " .
            " LEFT JOIN " . DB_PREFIX . "content_to_category p2c ON (p.content_id = p2c.content_id) " .
            " LEFT JOIN " . DB_PREFIX . "category_content cc ON (p2c.category_id = cc.category_id) " .
            " WHERE pd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND p2c.category_id = '" . (int)$category_id .  "'";

        $query = $this->db->query($sql);

        return $query->row['total'];
    }

    public function getTotalContent($data = array()) {
		$sql = "SELECT COUNT(*) AS total FROM " . DB_PREFIX . "content p " .
            " LEFT JOIN " . DB_PREFIX . "content_description pd ON (p.content_id = pd.content_id) " .
            " WHERE pd.language_id = '" . (int)$this->config->get('config_language_id') . "'";
		
		if (isset($data['filter_name']) && !is_null($data['filter_name'])) {
			$sql .= " AND LCASE(pd.name) LIKE '%" . $this->db->escape(strtolower($data['filter_name'])) . "%'";
		}
		if (isset($data['filter_status']) && !is_null($data['filter_status'])) {
			$sql .= " AND p.status = '" . (int)$data['filter_status'] . "'";
		}
        else{
            $sql .= " AND p.status = '1'";    
        }

		$query = $this->db->query($sql);
		
		return $query->row['total'];
	}	

	public function getTotalContentsByImageId($image_id) {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "content WHERE image_id = '" . (int)$image_id . "'");

		return $query->row['total'];
	}
    
    public function getContentCategoriesList($content_id, $show_in_menu = 0) {
        $sql = "SELECT * FROM " . DB_PREFIX . "category_content c " .
            " LEFT JOIN " . DB_PREFIX . "content_to_category  c2c ON (c.category_id = c2c.category_id)" .
            " LEFT JOIN " . DB_PREFIX . "category_content_description  ccd ON (c2c.category_id = ccd.category_id)" .
            " WHERE c2c.content_id = '" . (int)$content_id . "'" . 
            " ORDER BY c.sort_order ASC";   
        $query = $this->db->query($sql);
        
        $content_category_data = array();
        foreach ($query->rows as $result) {
            $content_category_data[] = array(
                'category_id'   => $result['category_id'],
                'image'         => $result['image'],
                'sort_order'    => $result['sort_order'],
                'name'          => $result['name'],
            );
        }
        
        return $content_category_data;
    }
}
?>